﻿using System.Linq;
using System.Text.RegularExpressions;

namespace XiaoSe.Validation
{
    /// <summary>
    ///Win Web 正则表达式验证
    /// </summary>
    public class DataValidation
    {
        private Regex _r;

        /// <summary>
        /// 验证是不是正整数，是返回True，不是返回False
        /// </summary>
        /// <returns>布尔值</returns>
        public bool IsPInt(string pIntString)
        {
            _r = new Regex(@"^[\d]{1,}$");
            try
            {
                return _r.Match(pIntString).Success;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 验证是不是整数，是返回True，不是返回False
        /// </summary>
        /// <param name="intString">要验证的字符串</param>
        /// <returns>布尔值</returns>
        public bool IsInt(string intString)
        {
            _r = new Regex(@"^-?[\d]+$");
            try
            {
                return _r.Match(intString).Success;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 验证是不是邮箱 ，是返回True，不是返回False
        /// </summary>
        /// <param name="emailString">要验证的字符串</param>
        /// <returns>布尔值</returns>
        public bool IsEmail(string emailString)
        {
            _r = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
            try
            {
                return _r.Match(emailString).Success;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 验证是不是手机或固话号码 ，是返回True，不是返回False
        /// </summary>
        /// <param name="tel">要验证的字符串</param>
        /// <returns>布尔值</returns>
        public bool IsTel(string tel)
        {
            _r = new Regex(@"\b(86)?0?\d{11}\b");
            try
            {
                return _r.Match(tel).Success;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        ///  验证是不是全是中文 ，是返回True，不是返回False
        /// </summary>
        /// <returns>布尔值</returns>
        public bool IsChinese(string chineseStr)
        {
            return RemoveChinese(chineseStr) == "";
        }

        /// <summary>
        /// 验证是不是全是字母，是返回True，不是返回False
        /// </summary>
        /// <param name="lettersStr">要验证的字符串</param>
        /// <returns>布尔值</returns>
        public bool IsLetters(string lettersStr)
        {
            return RemoveLetters(lettersStr) == "";
        }

        /// <summary>
        /// 去掉字符串里面的中文
        /// </summary>
        /// <param name="content">要去掉中文的字符串</param>
        /// <returns>字符串</returns>
        public string RemoveChinese(string content)
        {
            var mc = Regex.Matches(content, @"[\u4e00-\u9fa5]", RegexOptions.IgnoreCase);
            return mc.Cast<Match>().Aggregate(content, (current, c) => current.Replace(c.Value, ""));
        }
        /// <summary>
        /// 去掉字符串里面的字母
        /// </summary>
        /// <param name="content">要去掉字母的字符串</param>
        /// <returns>字符串</returns>
        public string RemoveLetters(string content)
        {
            var mc = Regex.Matches(content, @"[A-Za-z]", RegexOptions.IgnoreCase);
            return mc.Cast<Match>().Aggregate(content, (current, c) => current.Replace(c.Value, ""));
        }
    }
}
